package com.demo.controller;


import com.demo.controller.from.MovieAddMoneyFrom;
import com.demo.controller.from.MovieProductAddFrom;
import com.demo.exetion.MainExection;
import com.demo.model.MovieProduct;
import com.demo.service.MovieProductService;
import com.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-04-05
 */
@RestController
@RequestMapping("/movieProduct")
@Api(value = "电影",tags = "电影")
public class MovieProductController {

    @Autowired
    private MovieProductService movieProductService;

    @PostMapping("/addMovie")
    @ApiOperation(value = "创建电影",notes = "创建电影")
    @ApiImplicitParam(value = "鉴权token",name = "authentication_token",paramType  = "header", dataType = "String", required=true)
    public Result<Boolean> addMovie(@RequestBody MovieProductAddFrom movieProductAddFrom, BindingResult bindingResult) throws MainExection {
        if (bindingResult.hasErrors()) {
            throw new MainExection(500, "参数校验未通过");
        }
        Boolean aBoolean = movieProductService.addMovieProduct(movieProductAddFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }


    @PostMapping("/onTheShelf/{id}")
    @ApiOperation(value = "电影上线",notes = "电影上线")
    @ApiImplicitParam(value = "鉴权token",name = "authentication_token",paramType  = "header", dataType = "String", required=true)
    public Result<Boolean> onTheShelf(@PathVariable @NotNull @Min(1) Integer id) throws MainExection {
        Boolean aBoolean = movieProductService.onTheShelf(id);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping("/offTheShelf/{id}")
    @ApiOperation(value = "电影下线",notes = "电影下线")
    @ApiImplicitParam(value = "鉴权token",name = "authentication_token",paramType  = "header", dataType = "String", required=true)
    public Result<Boolean> offTheShelf(@PathVariable @NotNull @Min(1) Integer id) throws MainExection {
        Boolean aBoolean = movieProductService.offTheShelf(id);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping("/selectOneMovieProduct")
    @ApiOperation(value = "查询电影详情",notes = "查询电影详情")
    @ApiImplicitParam(value = "鉴权token",name = "authentication_token",paramType  = "header", dataType = "String", required=true)
    public Result<MovieProduct> selectOneMovieProduct(Integer id) throws MainExection {
        MovieProduct movieProduct = movieProductService.selectOneMovieProduct(id);
        if (movieProduct==null) throw new MainExection(500,"该电影不存在");
        return Result.getInstance(MovieProduct.class).setData(movieProduct);
    }


    @PostMapping("/addMoney")
    @ApiOperation(value = "票房更改",notes = "票房更改")
    @ApiImplicitParam(value = "鉴权token",name = "authentication_token",paramType  = "header", dataType = "String", required=true)
    public Result<Boolean> addMoney(@RequestBody MovieAddMoneyFrom movieAddMoneyFrom) throws MainExection {
        Boolean aBoolean = movieProductService.addMoney(movieAddMoneyFrom.getId(), Double.parseDouble(movieAddMoneyFrom.getMoney()));
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

}

